package com.iot.manage.biz.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;

import com.iot.common.core.exception.ModelException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.manage.api.domain.SysOrg;
import com.iot.manage.api.domain.SysUserOrg;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

@RestController
@RequiredArgsConstructor
@RequestMapping("/org")
@Api(value = "org", tags = "组织机构")
public class OrgController {

	@Setter
	private Throwable cause;

	@SecurityAuthorization
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<List<SysOrg>> list(@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "rootId", required = false, defaultValue = "-1") String rootId) throws Exception {
		List<SysOrg> sysOrgs = SysOrg.dao.find("SELECT T.*,(CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM "
				+ SysOrg.TABLE_NAME + " WHERE PARENTID=T.ID)>0 THEN 1 ELSE  0 END )  as hasChildren FROM "
				+ SysOrg.TABLE_NAME + " T WHERE T.PARENTID=? ORDER BY T.SORT ASC ", rootId);
		return R.ok(sysOrgs);
	}

	@SecurityAuthorization
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SysOrg> save(@Valid SysOrg sysOrg) {
		if (sysOrg == null) {
			throw new ModelException("对象不可以为空！");
		}
		if (StrKit.isBlank(sysOrg.getId())) {
			sysOrg.setId(UuidUtil.getUUID());
			if (sysOrg.save()) {
				return R.ok(sysOrg);
			}
		} else {
			if (sysOrg.update()) {
				return R.ok(sysOrg);
			}
		}
		return R.failed();
	}

	@SecurityAuthorization
	@GetMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		if (Db.queryDouble("SELECT COUNT(*) FROM " + SysOrg.TABLE_NAME + " WHERE PARENTID=?", id) > 0) {
			return R.failed("有子节点，不允许删除！", false);
		}
		if (SysOrg.dao.deleteById(id)) {
			Db.delete("DELETE FROM " + SysUserOrg.TABLE_NAME + " WHERE ORGID=? ", id);
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

	@SecurityAuthorization
	@GetMapping(value = { "/selectTree" }, produces = "application/json; charset=utf-8")
	public R<List<SysOrg>> selectTree(
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "rootId", required = false, defaultValue = "-1") String rootId) throws Exception {
		List<SysOrg> sysOrgs = SysOrg.dao.find("SELECT T.*,(CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM "
				+ SysOrg.TABLE_NAME + " WHERE PARENTID=T.ID)>0 THEN 1 ELSE  0 END )  as hasChildren FROM "
				+ SysOrg.TABLE_NAME + " T WHERE T.PARENTID=? ORDER BY T.SORT ASC ", rootId);
		return R.ok(sysOrgs);
	}

}
